from user.views import *
from user.models import *
from datetime import datetime


def HuiSer(query):
    data = []
    for i in query:
        js = {}
        js['update'] = i.update.split(' ')[0]
        js['upd'] = i.update
        js['json'] = eval(i.json)
        data.append(js)

    data = sorted(data, key=lambda x: datetime.strptime(x['upd'], '%Y-%m-%d %H:%M:%S'), reverse=True)
    for i in data:
        del i['upd']

    return data


def RTHuiSer(query):
    data = []
    for i in query:
        js = {}
        js['date'] = i.date
        js['time'] = i.time
        js['json'] = eval(i.json)
        data.append(js)

    return sorted(data, key=lambda x: datetime.strptime(x['time'], '%H:%M:%S'), reverse=True)


def TransactionOrderSer(query):
    data = {'buy': [], 'sell': [], 'sum_list': []}

    for j in query:
        js = {}
        js['id'] = j.id
        js['order_type'] = j.order_type
        js['price'] = j.price
        js['create'] = j.create
        js['number_shares'] = j.number_shares
        js['lock'] = false

        data[j['order_type']].append(js)
        data['sum_list'].append(js)

    data['buy'] = list(sorted(data['buy,'], key=lambda x: x['create']))
    data['sell'] = list(sorted(data['sell'], key=lambda x: -x['create']))
    data['sum_list'] = list(sorted(data['sum_list'], key=lambda x: x['create']))

    return data
